import org.junit.Test;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Stack;

/**
 * @program: guli_parent
 * @description:
 * @author: @why
 * @create: 2021-02-07 08:25
 **/
public class Solution {
    @Test
    public void m() {
       maxTurbulenceSize(new int[]{4,8,12,16});

    }
    public int maxTurbulenceSize(int[] arr) {
        if(arr.length==1) return 1;
        int left=0;
        int flag=0;
        int max=0;
        int right=0;
        while(right<arr.length-1){
            if(arr[left]==arr[left+1]&&left<=right){
                left++;
                max=Math.max(max,1);
            }
            if(flag%2==1){//当为第一种情况
                if(right%2==1){//当为奇数
                    if(arr[right]<=arr[right+1]){//不满足题意
                        max=Math.max(right-left+1,max);
                        left++;
                        right=left;
                        flag=right;
                        continue;
                    }

                }else if(right%2==0){  //当为偶数
                    if(arr[right]>=arr[right+1]){//不满足题意
                        max=Math.max(right-left+1,max);
                        left++;
                        right=left;
                        flag=right;
                        continue;
                    }
                }
            }else if(flag%2==0){  //第二章情况
                if(right%2==0){
                    if(arr[right]<=arr[right+1]){
                        max=Math.max(right-left+1,max);
                        left++;
                        right=left;
                        flag=right;
                        continue;
                    }
                }else {  //为奇数
                    if(arr[right]>=arr[right+1]){
                        max=Math.max(right-left+1,max);
                        left++;
                        right=left;
                        flag=right;
                        continue;
                    }
                }
            }
            right++;
        }
        return max;
    }

}
